using System;
using System.Windows.Forms;
using TXLooker.WorkItemExtention.Core;

namespace TXLooker.WorkItemExtention.UI
{
    public partial class WorkItemUpdateForm : Form
    {
        #region Class Properties

        public Core.WorkItemAppConfigs WorkItemAppConfigs
        {
            get 
            {
                if( this.m_configs == null )
                    return null;

                SetDataFromControls( ref this.m_configs );
                return this.m_configs; 
            }
            set 
            {
                if( value == null )
                    throw new NullReferenceException();

                this.m_configs = value;

                SetDataToControls(this.m_configs);
            }
        }
        private Core.WorkItemAppConfigs m_configs = null;

        #endregion Class Properties

        #region Constructors

        public WorkItemUpdateForm()
        {
            InitializeComponent();

            EventHandler handler =
                delegate( object sender, EventArgs args )
                {
                    this.m_automaticPanel.Enabled = object.ReferenceEquals( sender, this.m_rbtnAutomaticUpdate );
                };

            this.m_rbtnAutomaticUpdate.CheckedChanged += new EventHandler( handler );
            this.m_rbtnCustomUpdate.CheckedChanged += new EventHandler( handler );
        }
        public WorkItemUpdateForm(Core.WorkItemAppConfigs settings) : this()
        {
            this.WorkItemAppConfigs = settings;
        }

        #endregion Constructors

        protected override void OnLoad( EventArgs e )
        {
            base.OnLoad(e);
            this.CenterToScreen();

            if( this.WorkItemAppConfigs == null )
                this.WorkItemAppConfigs = new Core.WorkItemAppConfigs();
        }

        private void SetDataFromControls( ref Core.WorkItemAppConfigs workItemAppSettings )
        {
            workItemAppSettings.AutomaticUpdate = this.m_rbtnAutomaticUpdate.Checked;
            workItemAppSettings.AutomaticUpdatePeriod = (int)this.m_ntbUpdatePeriod.Value;
        }
        private void SetDataToControls( WorkItemAppConfigs workItemAppSettings )
        {
            this.m_rbtnAutomaticUpdate.Checked = workItemAppSettings.AutomaticUpdate;
            this.m_rbtnCustomUpdate.Checked = !workItemAppSettings.AutomaticUpdate;
            this.m_ntbUpdatePeriod.Value = workItemAppSettings.AutomaticUpdatePeriod;
        }
    }
}